package com.k2data.k2app.controller;

import com.github.pagehelper.PageHelper;
import com.k2data.k2app.constant.Constant;
import com.k2data.k2app.domain.SysMenu;
import com.k2data.k2app.mapper.SysMenuMapper;
import com.k2data.k2app.response.CommonResultResponse;
import com.k2data.k2app.response.PageInfo;
import com.k2data.k2app.response.ResponseHelper;
import com.k2data.k2app.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统菜单
 * Created by clb on 17-3-17.
 */
@Api(description = "系统菜单")
@RestController
@RequestMapping("/menus")
public class SysMenuController {
	private final SysMenuMapper sysMenuMapper;
	private final SysMenuService sysMenuService;
	@Autowired
	public SysMenuController(SysMenuMapper sysMenuMapper,SysMenuService sysMenuService) {
		this.sysMenuMapper = sysMenuMapper;
		this.sysMenuService = sysMenuService;
	}

	@ApiOperation(value = "创建")
	@ApiImplicitParam(name = "sysMenu", value = "sysMenu对象", required = true, dataType = "SysMenu", paramType = "body")
	@PostMapping(value = "", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<CommonResultResponse> post(@RequestBody SysMenu sysMenu) throws Exception {
		return ResponseHelper.success(sysMenuService.insert(sysMenu));
	}

	@ApiOperation(value = "标记为已删除")
	@ApiImplicitParam(name = "id", value = "任务id", required = true, dataType = "Long", paramType = "path")
	@PutMapping(value = "/deleted/{id}")
	public ResponseEntity<CommonResultResponse> delete(@PathVariable("id") Long id) throws Exception {
		SysMenu sysMenu = new SysMenu();
		sysMenu.setIsDelete(Constant.YES);
		sysMenu.setId(id);
		return ResponseHelper.success(sysMenuMapper.updateDeletedFlag(sysMenu));
	}

	@ApiOperation(value = "删除")
	@ApiImplicitParam(name = "id", value = "任务id", required = true, dataType = "Long", paramType = "path")
	@DeleteMapping(value = "/{id}")
	public ResponseEntity<CommonResultResponse> updateDeletedFlag(@PathVariable("id") Long id) throws Exception {
		return ResponseHelper.success(sysMenuMapper.deleteById(id));
	}

	@ApiOperation(value = "根据 id 更新")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path"),
			@ApiImplicitParam(name = "sysMenu", value = "sysMenu对象", required = true, dataType = "SysMenu", paramType = "body")
	})
	@PutMapping(value = "/{id}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<CommonResultResponse> update(@PathVariable("id") Long id,
													   @RequestBody SysMenu sysMenu) throws Exception {
		sysMenu.setId(id);
		return ResponseHelper.success(sysMenuService.update(sysMenu));
	}

	@ApiOperation(value = "根据id获取")
	@ApiImplicitParam(name = "id", value = "the value of id", required = true, dataType = "Long", paramType = "path")
	@GetMapping(value = "/{id}")
	public ResponseEntity<CommonResultResponse> get(@PathVariable("id") Long id) throws Exception {
		return ResponseHelper.success(sysMenuMapper.getById(id));
	}

	@ApiOperation(value = "获取树形菜单")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小(默认 30)", dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "parentId", value = "parent id", dataType = "Long", paramType = "query"),
			@ApiImplicitParam(name = "isTree", value = "是否返回树形结构", dataType = "Boolean", paramType = "query")
	})
	@GetMapping(value = "")
	public ResponseEntity<CommonResultResponse> menusTree(@RequestParam(value = "pageNum", required = false) Integer pageNum,
	                                                      @RequestParam(value = "pageSize", required = false) Integer pageSize,
	                                                      @RequestParam(value = "parentId", required = false) Long parentId,
														  @RequestParam(value = "isTree", required = false) Boolean isTree) throws Exception {
		List<SysMenu> list = sysMenuService.menuGroup(parentId, isTree);
		pageSize = pageSize == null ? 1000 : pageSize;
		pageNum = (pageNum == null) ? Integer.valueOf(1) : pageNum;
		PageHelper.startPage(pageNum, pageSize == null ? Constant.DEFAULT_PAGE_SIZE : pageSize);
		PageInfo pageInfo = new PageInfo(pageNum, pageSize, list.size());
		return ResponseHelper.success(list,pageInfo);
	}

}
